package com.xms.schema.service.entity;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.datacore.service.FinderService;
import com.xms.schema.entity.Entity;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * EntityFinderServiceImpl
 * </p>
 *
 * @author migo
 * @since 2021/9/2
 */
@CacheConfig(cacheNames = "schema.entity", keyGenerator = "CacheKeyGenerator")
@Service
public class EntityFinderService extends FinderService<Entity> implements IEntityFinderService {

    @Override
    @Cacheable(condition="#id != null")
    public Entity getById(UUID id) {
        return super.getById(id);
    }

    @Override
    @Cacheable(condition="#idList != null && #idList.size() > 0")
    public List<Entity> getByIds(Collection<UUID> idList) {
        return super.getByIds(idList);
    }

    @Override
    @Cacheable()
    public Entity getOne(Wrapper<Entity> queryWrapper) {
        return super.getOne(queryWrapper);
    }

    @Override
    @Cacheable()
    public int count() {
        return super.count();
    }

    @Override
    @Cacheable()
    public int count(Wrapper<Entity> queryWrapper) {
        return super.count(queryWrapper);
    }

    @Override
    @Cacheable()
    public List<Entity> list(Wrapper<Entity> queryWrapper) {
        return super.list(queryWrapper);
    }

    @Override
    @Cacheable()
    public List<Entity> all() {
        return super.list();
    }

    @Cacheable(condition="#name != null && #name.length() > 0")
    @Override
    public Entity getByName(String name) {
        return super.getService().getOne(new LambdaQueryWrapper<Entity>().eq(Entity::getName, name));
    }

    @Cacheable(condition="#name != null && #name.size() > 0")
    @Override
    public List<Entity> getByNames(Collection<String> name) {
        return super.getService().list(new LambdaQueryWrapper<Entity>().in(Entity::getName, name));
    }

    @Override
    public boolean existsName(String name) {
        return super.count(new LambdaQueryWrapper<Entity>().eq(Entity::getName, name)) > 0;
    }

    @Override
    public boolean existsName(String name, UUID existId) {
        LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Entity::getName, name)
        .ne(Entity::getEntityid, existId);
        return super.count(queryWrapper) > 0;
    }

    @Cacheable()
    @Override
    public List<Entity> getRelatedEntities(UUID entityid, int relationShipType, int cascadeLinkMask) {
        return null;
    }
}
